<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Đặng Khánh Trung
 * Date: 11/1/12
 * Time: 8:58 PM
 * To change this template use File | Settings | File Templates.
 */
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// import Joomla modelitem library
jimport('joomla.application.component.modelitem');
jimport('joomla.html.pagination');

/**
 * Timkiemnhanvien Model
 */
class VastModelTimkiemnhanvien extends JModelItem
{
    /**
     * @var string donvis
     */
    protected $hosocanbos;
    private $_total = null;
    private $_pagination = null;

    public function __construct()
    {
        parent::__construct();
        
        $config = JVastConfig::getConfig();
        
        $this->setState('limit', (isset($config['numOfItemsPerPage']) ? (int)$config['numOfItemsPerPage'] : JRequest::getVar('limit', 5, '', 'int')));
        $this->setState('limitstart', JRequest::getVar('limitstart', 0, '', 'int'));
        $session = JFactory::getSession();

        $hoten = JRequest::getString('hosocanboHoten', $session->get('timkiemnhanvien.hoten'));
        $gioitinh = JRequest::getInt('hosocanboGioitinh', $session->get('timkiemnhanvien.gioitinh'));
        $donvi = JRequest::getInt('hosocanboDonvi', $session->get('timkiemnhanvien.donvi'));
        $phong = JRequest::getInt('hosocanboPhong', $session->get('timkiemnhanvien.phong')); 
               
        $session->set('timkiemnhanvien.hoten', $hoten);
        $session->set('timkiemnhanvien.gioitinh', $gioitinh);
        $session->set('timkiemnhanvien.donvi', $donvi);
        $session->set('timkiemnhanvien.phong', $phong);
                
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
            
        $query->select('a.*');
        $query->from('#__hosocanbo AS a');
        $query->join('INNER', '#__congtac AS b ON b.hosocanbo_id = a.id');
        $query->join('LEFT', '#__donvi AS c ON c.id = b.donvi_id');
        $query->join('LEFT', '#__phong AS d ON d.id = b.phong_id');
        
        if (!is_null($gioitinh) && $gioitinh != 2) {
            $query->where('a.gioitinh = ' . $gioitinh);
        }
            
        if (!is_null($donvi) && $donvi != -1) {
            $query->where('b.donvi_id = ' . $donvi);
        }

        if (!is_null($phong) && $phong != -1) {
            $query->where('b.phong_id = ' . $phong);
        }

        if (!is_null($hoten) && $hoten !== '') 
            $query->where('a.hoten like \'%' . $hoten . '%\'');
        elseif ($hoten === '')
            $query->where('a.hoten like \'%\'');
        
        $db->setQuery($query);
                
        $this->hosocanbos = $db->loadObjectList();
            
        $this->_total = count($this->hosocanbos);          
    }

    /**
     * Get the message
     * @return string The message to be displayed to the user
     */
    public function getHosocanbos()
    {
        $session = JFactory::getSession();
        $hoten = $session->get('timkiemnhanvien.hoten');
        $gioitinh = $session->get('timkiemnhanvien.gioitinh');
        $donvi = $session->get('timkiemnhanvien.donvi');
        $phong = $session->get('timkiemnhanvien.phong');
        
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);

        $limit = $this->getState('limit');
        $limitstart = $this->getState('limitstart');

        $query->select('a.*');
        $query->from('#__hosocanbo AS a');
        $query->join('INNER', '#__congtac AS b ON b.hosocanbo_id = a.id');
        $query->join('LEFT', '#__donvi AS c ON c.id = b.donvi_id');
        $query->join('LEFT', '#__phong AS d ON d.id = b.phong_id');

        if (!is_null($gioitinh) && $gioitinh != 2) {
            $query->where('a.gioitinh = ' . $gioitinh);
        }

        if (!is_null($donvi) && $donvi != -1) {
            $query->where('b.donvi_id = ' . $donvi);
        }

        if (!is_null($phong) && $phong != -1) {
            $query->where('b.phong_id = ' . $phong);
        }
            
        if (!is_null($hoten) && $hoten !== '') 
            $query->where('a.hoten like \'%' . $hoten . '%\'' . ' LIMIT ' . $limitstart . ',' . $limit);
        elseif ($hoten === '')
            $query->where('a.hoten like \'%\' LIMIT ' . $limitstart . ',' . $limit);
                    
        $db->setQuery($query);
        $this->hosocanbos = $db->loadObjectList();
                
        return $this->hosocanbos;
    }
    
    public function getPagination()
    {
        $this->_pagination = new JPagination($this->getTotal(), $this->getState('limitstart'), $this->getState('limit'));
        return $this->_pagination;
    }

    public function getTotal()
    {
        return $this->_total;
    }
    
    public function getPhong()
    {
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
    
        $query->select('a.*');
        $query->from('#__phong AS a');
        
        $db->setQuery($query);
        
        return $db->loadObjectList();            
    }

    public function getDonvi()
    {
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
    
        $query->select('a.*');
        $query->from('#__donvi AS a');
        
        $db->setQuery($query);
        
        return $db->loadObjectList();            
    }}